//
//  PlanPeopleCenterView.m
//  WanCaiC8
//
//  Created by 罗先森 on 2018/9/18.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "PlanPeopleCenterView.h"

#import "AdvertisingView.h"
#import "LabCell.h"
#import "MyVM.h"

#import "InformationVM.h"

@interface PlanPeopleCenterView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView  *collectionView;
@property (nonatomic ,strong) NSArray           *dataArr;

@property (nonatomic ,strong) AdvertisingView           *oneAdvertisingView;
@property (nonatomic ,strong) AdvertisingView           *twoAdvertisingView;
@property (nonatomic ,strong) AdvertisingView           *threeAdvertisingView;



@property (nonatomic ,strong)QXLable     *labPredictedResults;



@end


@implementation PlanPeopleCenterView

- (instancetype)init{
    if (self=[super init]) {

        self.backgroundColor=[UIColor whiteColor];
        /*-----------*/
        
        
        [self addSubview:self.oneAdvertisingView];
        
        [self.oneAdvertisingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(10);
            make.top.equalTo(self).offset(5);
            make.height.mas_equalTo(0);
        }];
        
        QXLable *labPredict=[[QXLable alloc]init];
        labPredict.text=@"预测期数:";
        [self addSubview:labPredict];
        [labPredict mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.top.equalTo(self.oneAdvertisingView.mas_bottom).offset(10);
        }];
        
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labPredict.mas_right).with.offset(5);
            make.centerY.equalTo(labPredict);
            make.right.equalTo(self);
            make.height.mas_offset(real_size(25));
        }];
        
        
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(labPredict.mas_bottom).with.offset(real_size(12));
            make.height.mas_equalTo(@(1));
        }];
        
        
        [self addSubview:self.twoAdvertisingView];
        
        [self.twoAdvertisingView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(10);
            make.top.equalTo(line.mas_bottom).offset(5);
            make.height.mas_equalTo(@0);
        }];
        
        
        self.labPredictedResults=[[QXLable alloc]init];
        [self addSubview:self.labPredictedResults];
        
        [self.labPredictedResults sizeToFit];
        [self.labPredictedResults mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(12);
            make.right.equalTo(self).offset(-6);
            make.top.equalTo(self.twoAdvertisingView.mas_bottom).with.offset(5);
        }];
        
        UIView *line1=[[UIView alloc]init];
        line1.backgroundColor=DEFAULTBGCOLOR;
        [self addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.top.equalTo(self.labPredictedResults.mas_bottom).offset(5);
            make.height.mas_equalTo(@(1));
        }];
        
        
        [self addSubview:self.threeAdvertisingView];
        [self.threeAdvertisingView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-10);
            make.left.equalTo(self).offset(10);
            make.top.equalTo(line1.mas_bottom).offset(2);
            make.height.mas_equalTo(@0);
        }];

    }
    return self;
}



- (AdvertisingView *)oneAdvertisingView{
    if (!_oneAdvertisingView) {
        WS(weakSelf);
        _oneAdvertisingView =[[AdvertisingView alloc]init];
        self.oneAdvertisingView.changeHeight = ^(float height) {
            [weakSelf.oneAdvertisingView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(height));
            }];
        };
    }
    return _oneAdvertisingView;
}
- (AdvertisingView *)threeAdvertisingView{
    if (!_threeAdvertisingView) {
        WS(weakSelf);
        _threeAdvertisingView =[[AdvertisingView alloc]init];
        self.threeAdvertisingView.changeHeight = ^(float height) {
            [weakSelf.threeAdvertisingView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(height));
            }];
        };
    }
    return _threeAdvertisingView;
}

- (AdvertisingView *)twoAdvertisingView{
    if (!_twoAdvertisingView) {
        WS(weakSelf);
        _twoAdvertisingView =[[AdvertisingView alloc]init];
        self.twoAdvertisingView.changeHeight = ^(float height) {
            [weakSelf.twoAdvertisingView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(@(height));
            }];
        };
        
    }
    return _twoAdvertisingView;
}


#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(real_size(50), real_size(25))];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];
    }
    return _collectionView;
}


#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
    [cell setWhiteBGColorWithTitle:self.dataArr[indexPath.row]  andIsSelect:indexPath.row==self.selected];
    
    cell.labTitle.font=[UIFont systemFontOfSize:10];
    
    return cell;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(0, 0, 0, 10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    self.selected=indexPath.row;
    [collectionView reloadData];
    
    
    //发送通知

    NSDictionary *dic = @{@"selected":[NSString stringWithFormat:@"%zd",indexPath.row]};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PlanPeopleCenterView_collectionView" object:nil userInfo:dic];
 
    [SVProgressHUD show];
    
    
}


- (NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr=@[@"10期",@"20期",@"30期",@"50期",@"100期"];
    }
    return _dataArr;
}

-(void)setArr1:(NSMutableArray *)arr1{
    _arr1 = arr1;
    self.oneAdvertisingView.dataArr = self.arr1;
    
    self.oneAdvertisingView.changeHeight(self.arr1.count*40);
}
-(void)setArr2:(NSMutableArray *)arr2
{
    
    _arr2 =arr2;
    self.twoAdvertisingView.dataArr = self.arr2;

    self.twoAdvertisingView.changeHeight(self.arr2.count*40);
}

-(void)setArr3:(NSMutableArray *)arr3{
    
    _arr3 =arr3;
    self.threeAdvertisingView.dataArr = self.arr3;

   self.threeAdvertisingView.changeHeight(self.arr3.count*40);
}


-(void)setModelArr1:(NSMutableArray<AdvertisingModel *> *)modelArr AndRowCount:(NSInteger)rowCount{
  
    _arr1 = modelArr;

    if (modelArr.count>0) {
        
        self.oneAdvertisingView.dataArr = modelArr;
        
        self.oneAdvertisingView.changeHeight(rowCount*40);
        
    }else{
        self.oneAdvertisingView.hidden = YES;
        
        self.oneAdvertisingView.changeHeight(0);
    }
}
-(void)setModelArr2:(NSMutableArray<AdvertisingModel *> *)modelArr AndRowCount:(NSInteger)rowCount{
    _arr2 = modelArr;
    
    if (modelArr.count>0) {
        
        self.twoAdvertisingView.dataArr = self.arr2;
        
        self.twoAdvertisingView.changeHeight(rowCount*40);
        
    }else{
        self.twoAdvertisingView.hidden = YES;
        
        self.twoAdvertisingView.changeHeight(0);
    }
}
-(void)setModelArr3:(NSMutableArray<AdvertisingModel *> *)modelArr AndRowCount:(NSInteger)rowCount{

    _arr3 = modelArr;
    if (modelArr.count>0) {

        self.threeAdvertisingView.dataArr = self.arr3;
        self.threeAdvertisingView.changeHeight(rowCount*40);
        
    }else{
        self.threeAdvertisingView.hidden = YES;
        
        self.threeAdvertisingView.changeHeight(0);
    }
}



-(void)setPredictedStr:(NSString *)predictedStr{
    _predictedStr = predictedStr;
//    NSString *strDesc=[returnValue objectForKey:@"Desc"];
    NSString *strContent=[predictedStr stringByReplacingOccurrencesOfString:@"<font>" withString:@"<font color='#f13131'>"];
    strContent=[NSString stringWithFormat:@"%@",strContent];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[strContent dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    
    self.labPredictedResults.attributedText = attrStr;
    self.labPredictedResults.font=[UIFont systemFontOfSize:13];
    
}


@end
